# -*- coding: utf-8 -*-
'''
Module name: KIWIImport

Created on 2013-3-13

@author: yangyil
'''
from DATAImport import DATAImport
import cx_Oracle
import string

class KIWIImport(DATAImport):
    '''
    The main class of KIWI data importing.
    '''
    fields = "(LEVEL_NO INT,PARCEL_ID INT,PARCEL_DIVIDE INT,PARCEL_DH BLOB,ROAD BLOB,BKGD BLOB,NAME BLOB,INFRA BLOB,GUIDE BLOB,STRING BLOB,ABOUND BLOB)"
    
    def _ReadOneRow(self):
        '''
        Read one row  from the source file.
        '''
        row = []
        binary_var = self._cursor.var(cx_Oracle.BLOB)
        for _ in range(3):
            temp = string.atoi(self._srcReader.ReadBy(','))
            row.append(temp)
        DHBuff,DFSize = self._GetSizeFromDH()
        binary_var.setvalue(0, DHBuff)
        row.append(binary_var)
        for size in DFSize:
            binary_var.setvalue(0, self._srcReader.ReadBLOB(size))
            row.append(binary_var)
        return row
                    
    def _GetSizeFromDH(self):
        '''
        Return two value:
        1.The buffer of 'PARCEL_DH';
        2.The sizes of DFs(such as road、guide).
        '''
        rList = []
        DHcursor = self._srcReader.GetCursor()
        check = self._srcReader.Read()
        if check != '\"':
            raise "ERROR: unexpected char , should be '\"'"
        headsize = self._srcReader.getSWS()
        if headsize == 16**2 - 1:
            raise "ERROR: unexpected headersize(0xFFFF)"
        self._srcReader.SetCursor(DHcursor+1+34)#経路計算データリージョン数的位置
        routePlanRecCnt = self._srcReader.getN(2)
        self._srcReader.SetCursor(self._srcReader.GetCursor()+routePlanRecCnt*4)
        for _ in range(7):
            self._srcReader.Read(4)
            rList.append(self._srcReader.getSWS())
        self._srcReader.SetCursor(DHcursor)
        rBuff = self._srcReader.ReadBLOB(headsize)
        return rBuff,rList